package com.huo.com.redisLock.redislistener;

import com.alibaba.fastjson.JSON;
import com.huo.com.redisLock.service.TestStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Map;


@Component
public class MyListener implements MessageListener {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private TestStockService testStockService;

    @Override
    public void onMessage(Message message, byte[] bytes) {

        String channel=new String(message.getChannel());//获取通道
        if(channel.equals("LOCK")){
           try{
               String json=new String(message.getBody());
               //System.out.println(json);
               Map<String,String> map =(Map<String,String>)JSON.parseObject(json, Map.class);
               //在进行获取锁的操作
               testStockService.getStock(map);
               json=null;
           }catch (Exception e){
               e.printStackTrace();
           }finally {
               channel=null;
           }
        }
    }
}
